package com.tjsinfo.tjsoa.fileDocument.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.DealString;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.fileDocument.bo.IFileDocumentFileBo;
import com.tjsinfo.tjsoa.fileDocument.bo.IFileDocumentGroupBo;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentFile;
import com.tjsinfo.tjsoa.system.bo.ISystemUserBo;
import com.tjsinfo.tjsoa.system.vo.Files;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;

public class UploadAllAction extends Action{

	private IFileDocumentGroupBo documentGroupBo;
	private ISystemUserBo tjsUserBo;
	private IFileDocumentFileBo documentFileBo;
	
	public IFileDocumentGroupBo getDocumentGroupBo() {
		return documentGroupBo;
	}

	public void setDocumentGroupBo(IFileDocumentGroupBo documentGroupBo) {
		this.documentGroupBo = documentGroupBo;
	}

	public ISystemUserBo getTjsUserBo() {
		return tjsUserBo;
	}

	public void setTjsUserBo(ISystemUserBo tjsUserBo) {
		this.tjsUserBo = tjsUserBo;
	}

	public IFileDocumentFileBo getDocumentFileBo() {
		return documentFileBo;
	}

	public void setDocumentFileBo(IFileDocumentFileBo documentFileBo) {
		this.documentFileBo = documentFileBo;
	}

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		Date date = new Date();
		TSystemUser faUser = tjsUserBo
				.findByIdSystemUser(user
						.getUser_id());
		String subject = "";
		String groupid = "";
		String keyword = "";
		String beizhu = "";
		beizhu = request.getParameter("beizhu");
		if (beizhu != null && !"".equals(beizhu)) {
			beizhu = beizhu.replace("\r\n","<br/>");
			beizhu = beizhu.replace("\"", "“");
		}
		subject = request.getParameter("subject");
		subject = new String(subject.getBytes("ISO-8859-1"), "UTF-8"); 
		groupid = new String(request.getParameter("groupid")
				.getBytes("ISO-8859-1"), "UTF-8");
		
		//request.getSession().getAttribute("dataPath")+
		String fromFilePath=ToolKit.getProperties("com/tjsinfo/tjsoa/system/properties/systemResource.properties", this).getProperty("system.uploadFile.tempFilePath");
		String toFilePath =ToolKit.getProperties("com/tjsinfo/tjsoa/fileDocument/properties/systemResource.properties", this).getProperty("fileupload");
		File file = new File(toFilePath);
		if(!file.exists()){
			file.mkdir();
		}
		List<Files> fileList=DealString.fileCopyTofile(request, fromFilePath, toFilePath);
		List listFileName = new ArrayList();
		for (Files files : fileList) {
			FileDocumentFile documentFile1 = new FileDocumentFile();
			documentFile1.setId(files.getNewFileName().substring(0,files.getNewFileName().indexOf(".")).replace("-",""));
			documentFile1.setTjsUser(faUser);
			documentFile1.setFileName(files.getOldFileName());
			documentFile1.setSubject(subject);
			documentFile1.setKeyword(keyword);
			documentFile1.setDocket(beizhu);
			documentFile1.setDatetime(date);
			documentFile1.setFilePath(files.getToFilePath());
			documentFile1.setState(Integer
					.valueOf(1));
			documentFile1.setFileSize(Integer.valueOf(files.getFileSize()));
			documentFile1.setFileDocumentGroup(documentGroupBo.findById(groupid));
			listFileName.add(files.getOldFileName());
			documentFileBo.save(documentFile1);
			
		}
		
		// 转向，并传递提示语
		request.setAttribute("listFileName", listFileName);
		request.setAttribute("returnString", "");
		return mapping.findForward("ok");
	}

	
}
